package com.example.health;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper{
	
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "medicalStaffDb";
 
    // Contacts table name
    private static final String TABLE_CONTACTS = "medicalStaff";
 
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_FIRSTNAME = "first_name";
    private static final String KEY_LASTNAME = "last_name";
    private static final String KEY_TITLE = "title";
    private static final String KEY_TYPE = "type";
    private static final String KEY_LASTUPDATA = "last_update";
 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_USERNAME + " TEXT,"
                + KEY_PASSWORD + " TEXT," + KEY_FIRSTNAME + " TEXT," + KEY_LASTNAME + " TEXT,"
                + KEY_TITLE + " TEXT," + KEY_TYPE + " TEXT," + KEY_LASTUPDATA + " DATETIME" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
 
        // Create tables again
        onCreate(db);
    }
    
    
    // Getting single contact
    public String login(String username, String password) {
    	SQLiteDatabase db = this.getReadableDatabase();
    	 
        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_USERNAME, KEY_LASTNAME,
                KEY_FIRSTNAME, KEY_LASTNAME, KEY_LASTUPDATA, KEY_TITLE, KEY_TYPE}, KEY_USERNAME + "=? and "+ KEY_PASSWORD + "=?",
                new String[] { username, password }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        if(cursor.getCount() > 0)
        	return "success";
        else
        	return "fail";
    }
    
    void addMedicalStaff(MedialStaff contact) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_USERNAME, contact.getUsername()); // Staff Username
        values.put(KEY_PASSWORD, contact.getPassword()); // Staff Password
        values.put(KEY_FIRSTNAME, contact.getFirstName()); // Staff first name
        values.put(KEY_LASTNAME, contact.getLastName()); // Staff last name
        values.put(KEY_LASTUPDATA, contact.getLastUpdate().toString()); // Staff last update
        values.put(KEY_TITLE, contact.getTitle()); //Staff title
        values.put(KEY_TYPE, contact.getType()); //Staff type
        
 
        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

}
